/**
 * $Id$
 *
 * Copyright (c) 2014 ChinaSoft International, Ltd. All rights reserved
 * JointForce Project
 *
 */
package com.csi.jf.crucible.api.crucible;

/**
 * @Title: ApiTestCrowdUsers.java
 * @Description: <br>
 *               <br>
 * @Company: Chinasofti
 * @Created on 2014-7-4 下午3:25:00
 * @author zhangmh
 * @version $Revision: 1.0 $
 * @since 1.0
 */
public class ApiTestCruUsers {
    
    private RestClient client = new RestClient();
    
    public static void main(String[] args) throws Exception{
        ApiTestCruUsers test = new ApiTestCruUsers();
        test.getLoginToken("zhangmh","123456");
    }
    
    /**
     * @param string username
     * @param string password
     * @return
     *{
     *  "token":"zhangmh:2:d249481bd97fc34edc6e855731168517"
     * }
     * @throws Exception
     */
    public void getLoginToken(String username,String password)throws Exception{
        String url = "/rest-service/auth-v1/login?userName="+username+"&password="+password;
        client.doGet(url,false);
    }
    
    /**
     {
        "userData": [
            {
                "userName": "zhangmh", 
                "displayName": "daniel zang", 
                "avatarUrl": "http://daniel-pc:8060/avatar/zhangmh?s=48", 
                "SONAR_URL": "/user/zhangmh"
            }
        ], 
        "errors": { }
     }
     * @throws Exception
     */
    public void users_v1() throws Exception{
        String url = "/rest-service/users-v1?username=zhangmh&"+RestClient.CRUCIBLE_AUTH_TOKEN;
        client.doGet(url);
        //String SONAR_URL = "/rest-service/users-v1?username=zhangmh&"+client.AUTH_URL;
        //client.doGet(SONAR_URL)
    }
    
    public void addUser() throws Exception{
        //type=built-in&crucible=false&
        String url = "/rest-service-fecru/admin/users?type=built-in&crucible=false&"+RestClient.CRUCIBLE_AUTH_TOKEN;
        /*{
        "name" : "newkash",
        "password" : "1234",
        "displayName" : "Lukasz Kuzynski",
        "email" : "newkash@atlassian.com",
        "type" : "built-in",
        "crucible" : true
          }*/
        String json = "{\"name\" : \"newkash\",\"password\" : \"123456\",\"displayName\" : \"Lukasz Kuzynski\",\"email\" : \"newkash@atlassian.com\",\"type\" : \"built-in\",\"crucible\" : true}";
        client.doPost(url,json);
    }
    
    /**
     *@param type string filter users by authentication type: built-in, LDAP, host, custom, ajp, crowd
     *@param crucible boolean filter users by crucible flag
     *@return:
     {
        "start": 0, 
        "limit": 100, 
        "lastPage": true, 
        "size": 4, 
        "values": [
            {
                "name": "duanhui", 
                "displayName": "duan hui", 
                "email": "duanhui@chinasofti.com", 
                "type": "crowd", 
                "crucible": true
            }, 
            {
                "name": "fenghaibing", 
                "displayName": "feng haibing", 
                "email": "fenghaibing@chinasofti.com", 
                "type": "crowd", 
                "crucible": true
            }, 
            {
                "name": "maogx", 
                "displayName": "mao gx", 
                "email": "maogx@chinasofti.com", 
                "type": "crowd", 
                "crucible": true
            }, 
            {
                "name": "zhangmh", 
                "displayName": "daniel zang", 
                "email": "zhangmh_hunu@126.com", 
                "type": "crowd", 
                "crucible": true
            }
        ]
     }
     */
    public void getAdminUser() throws Exception{
        String url = "/rest-service-fecru/admin/users?type=crowd&crucible=true&"+RestClient.CRUCIBLE_AUTH_TOKEN;
        //for test
        client.doGet(url);
    }

}
